import { NameSpace } from 'utils';
import { Message } from '@teamix/ui';
import exceed from 'utils/apimap';
import { getProjectServices, saveProjectServices, postServiceLog } from 'api';
import intl from '../../../../../../../../../locale';

const ns = NameSpace('PROJECT_SETTINGS_SERVICES');

export const UPDATE_DATA = ns('UPDATE_DATA');
export const RESET_DATA = ns('RESET_DATA');

export function updateData(data) {
  return (dispatch) => {
    dispatch({
      type: UPDATE_DATA,
      data,
    });
  };
}

export function resetData() {
  return (dispatch) => {
    dispatch({
      type: RESET_DATA,
    });
  };
}

// 获取设置
export function getInfo() {
  return (dispatch) => {
    updateData({
      isLoadingService: true,
    })(dispatch);
    getProjectServices()
      .then((result) => {
        updateData({
          data: result.integration_service_setting || {},
          isLoadingService: false,
        })(dispatch);
      })
      .catch((err) => {
        updateData({
          isLoadingService: false,
        })(dispatch);
        console.error(err);
      });
  };
}

// 修改设置
export function saveInfo(params, cb, okFn = null, failFn = null) {
  return (dispatch) => {
    updateData({
      isPostingService: true,
    })(dispatch);
    saveProjectServices(params)
      .then((result) => {
        Message.success({
          title: intl.get({
            id: 'code-assets.services.actions.SavedSuccessfully',
            defaultMessage: '保存成功',
          }),
        });

        updateData({
          isPostingService: false,
          data: result.integration_service_setting || {},
        })(dispatch);
        if (okFn != null) {
          okFn();
        }
        cb && cb(true);
      })
      .catch((err) => {
        updateData({
          isPostingService: false,
        })(dispatch);
        console.error(err);
        if (err.response) {
          const rsp = JSON.parse(err.response);
          if (rsp.cause) {
            if (failFn != null) {
              failFn(rsp);
            } else {
              Message.error({
                title: rsp.message,
              });
            }
          }
        }
        cb && cb(false);
      });
  };
}

// 开启或者关闭服务
export function toogleService(params) {
  return () => {
    postServiceLog(params)
      .then(() => {})
      .catch((err) => {
        console.error(err);
      });
  };
}

// 获取敏感信息服务设置
export function getOrgServiceSetting() {
  return (dispatch) => {
    const { organization_id: org_id } = window.FORCE.organization;
    exceed
      .forceFetch({
        api: 'org.service.get',
        params: {
          orgId: org_id,
        },
      })
      .then((result) => {
        updateData({
          org_sensitive_info_detection: result.sensitive_info_detection,
        })(dispatch);
      });
  };
}

// 文档开关
export function setExtensionService(enabled, cb) {
  return () => {
    const { pathResource } = window.FORCE;
    exceed
      .fetch({
        api: 'uiless.enableExtension',
        params: {
          organizationId: window.UILessConfig.orgId,
          objectId: pathResource.id,
          appId: '5e84413ee10f6441476e724c',
        },
        data: {
          objectType: 'Repository',
          enabled,
        },
      })
      .then((res) => {
        if (res.success) {
          Message.success({
            title: res.message,
            duration: 1000,
          });
          cb && cb();
        } else {
          Message.error({
            title: res.message,
            duration: 1000,
          });
        }
      });
  };
}
